package cn.wolfcode.crud.service.impl;

import cn.wolfcode.crud.domain.SystemDictionaryItem;
import cn.wolfcode.crud.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SystemDictionaryItemImpl implements ISystemDictionaryItemService {
    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;


    public void save(SystemDictionaryItem systemDictionaryItem) {
        //如果编号为空,去数据库查一个当前职业最大的序号.设置进去
         if(systemDictionaryItem.getSequence()== null){
               int max = systemDictionaryItemMapper.queryMaxSequenceById(systemDictionaryItem.getParentId());
                 systemDictionaryItem.setSequence(max+1);
         }
        systemDictionaryItemMapper.insert(systemDictionaryItem);
    }

    public void delete(Long id) {
        systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }

    public void update(SystemDictionaryItem systemDictionaryItem) {
        systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
    }

    @Transactional(readOnly = true)
    public SystemDictionaryItem get(Long id) {
        SystemDictionaryItem systemDictionaryItem= systemDictionaryItemMapper.selectByPrimaryKey(id);
        return systemDictionaryItem;
    }

    @Transactional(readOnly = true)
    public List<SystemDictionaryItem> listAll() {
        List<SystemDictionaryItem> systemDictionaryItem = systemDictionaryItemMapper.selectAll();
        return systemDictionaryItem;
    }

    @Override
    public PageInfo<SystemDictionaryItem> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
       List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectForList(qo);
        return new PageInfo<SystemDictionaryItem>(systemDictionaryItems);
    }

    @Override
    public List<SystemDictionaryItem> selectByParentSn(String sn) {
        return systemDictionaryItemMapper.selectByParentSn(sn);
    }


}
